// hain.cpp: implementation of the Chain class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Cells.h"


//////////////////////////////////////////////////////////////////////
// Cells
//////////////////////////////////////////////////////////////////////
Cells::Cells()
{
    SetRow(6);
	SetCol(24);
	SetRowSpan(4);
}

CRect Cells::GetCellRect(int row, int col)
{
    return Grid::GetCellRect(row, 23-col);
}

void Cells::DrawCells(CDC *pDC)
{
	CPen *pOldPen = pDC->SelectObject(&Sheet::txpen);
	Grid::DrawGrid(pDC);
    pDC->SelectObject(pOldPen);
}

void Cells::ShowData(CDC *pDC)
{
    CRect rect;
    CString str, str1;

    COLORREF oldcolor = pDC->GetTextColor();

    for(int col = 0; col < 24; col++)
    {
        pDC->SetTextColor(COLORREF(0));

        // basic
        rect = GetCellRect(0, col);
        getRiverBasicAt(col, str);
        if(!str.IsEmpty())
        {
            pDC->DrawText(str, &rect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
        }

        rect = GetCellRect(1, col);
        getLakeBasicAt(col, str);
        if(!str.IsEmpty())
        {
            pDC->DrawText(str, &rect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
        }

        pDC->SetTextColor(Sheet::colorGreen);
        // detail
        getRiverDetailAt(col, str, str1);
        rect = GetCellRect(2, col);
        if(!str.IsEmpty())
        {
            pDC->DrawText(str, &rect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
        }
        rect = GetCellRect(3, col);
        if(!str.IsEmpty())
        {
            pDC->DrawText(str1, &rect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
        }


        getLakeDetailAt(col, str, str1);
        rect = GetCellRect(4, col);
        if(!str.IsEmpty())
        {
            pDC->DrawText(str, &rect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
        }
        rect = GetCellRect(5, col);
        if(!str.IsEmpty())
        {
            pDC->DrawText(str1, &rect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
        }

        pDC->SetTextColor(Sheet::colorYellow);

        /*
        // river 3
        rect = GetCellRect(6, col);
        getCureAt(DataPool::RiverFirst, col, str);
        if(!str.IsEmpty())
        {
            pDC->DrawText(str, &rect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
        }

        rect = GetCellRect(7, col);
        getCureAt(DataPool::RiverSecond, col, str);
        if(!str.IsEmpty())
        {
            pDC->DrawText(str, &rect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
        }

        rect = GetCellRect(8, col);
        getCureAt(DataPool::RiverThird, col, str);
        if(!str.IsEmpty())
        {
            pDC->DrawText(str, &rect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
        }

        pDC->SetTextColor(Sheet::colorCyan);
        // lake 3
        rect = GetCellRect(9, col);
        getCureAt(DataPool::LakeFirst, col, str);
        if(!str.IsEmpty())
        {
            pDC->DrawText(str, &rect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
        }

        rect = GetCellRect(10, col);
        getCureAt(DataPool::LakeSecond, col, str);
        if(!str.IsEmpty())
        {
            pDC->DrawText(str, &rect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
        }

        rect = GetCellRect(11, col);
        getCureAt(DataPool::LakeThird, col, str);
        if(!str.IsEmpty())
        {
            pDC->DrawText(str, &rect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
        }*/
    }

    pDC->SetTextColor(oldcolor);
}

void Cells::getRiverBasicAt(int index, CString &str)
{
    Num num(NumQueue::getInstance()->GetAt(index));

    str.Empty();
    if(num.IsValid())
    {
        if(num.IsKindOf(V1))
            str = "A";
        else if(num.IsKindOf(V2))
            str = "B";
        else if(num.IsKindOf(V3))
           str = "C";
    }
}

void Cells::getLakeBasicAt(int index, CString &str)
{ 
    Num num(NumQueue::getInstance()->GetAt(index));

    str.Empty();
    if(num.IsValid())
    {
        if(num.IsKindOf(H1))
            str = "1";
        else if(num.IsKindOf(H2))
            str = "2";
        else if(num.IsKindOf(H3))
            str = "3";
    }
}

void Cells::getRiverDetailAt(int index, CString &key, CString &value)
{
    DataPool* dataPool = DataServer::getInstance()->getPool(DataPool::River);
    RiverPool* riverPool = (RiverPool*)dataPool;

    Item item = riverPool->getAt(index);

    key.Empty();
    value.Empty();
    if(item.first!=Total)
    {
        if(item.first==V1)
            key = "A";
        else if(item.first==V2)
            key = "B";
        else if(item.first==V3)
            key = "C";

        value.Format("%d", item.second);
    }
}

void Cells::getLakeDetailAt(int index, CString &key, CString &value)
{
    DataPool* dataPool = DataServer::getInstance()->getPool(DataPool::Lake);
    LakePool* lakelePool = (LakePool*)dataPool;

    Item item = lakelePool->getAt(index);

    key.Empty();
    value.Empty();
    if(item.first!=Total)
    {
        if(item.first==H1)
            key = "[1]";
        else if(item.first==H2)
            key = "[2]";
        else if(item.first==H3)
            key = "[3]";

        value.Format("%d", item.second);
    }
}

void Cells::getCureAt(int poolName, int index, CString &str)
{
    DataPool* dataPool = DataServer::getInstance()->getPool(DataPool::PoolName(poolName));
    CurePool* curePool = (CurePool*)dataPool;

    str.Empty();
    if( index < curePool->getSize() )
    {
        int value;
        //if(curePool->getSize() >= 24)
        //{
            //value = curePool->getAt(23);
            //value = curePool->getAt(index) - value;
        //}
        //else
        {
            value = curePool->getAt(index);
        }

        str.Format("%d", value);
    }
}
